<?php

namespace backend\controllers;

use backend\models\LoginForm;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

/**
 * Site controller
 */
class TestController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => [],
                        'allow' => true,
                    ],
                    [
                        'actions' => [],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['get'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        $excelReader = \PHPExcel_IOFactory::createReader('Excel5');
        return $this->render('index');
    }

    public function hdtExcelCallBack($data)
    {
        array_shift($data);//去除excel第一行列名
        $data = $this->Analysis(['id', 'name', 'value'], $data);
        $success_count = $this->saveData($data);
        return json_encode(['status' => 200,
            'message' => '成功',
            [
                'success_count' => $success_count,
                'all_count' => count($data)
            ]
        ]);
//        return $this->showResult(200, '成功', ['success_count' => $success_count, 'all_count' => count($data)]);
    }


    /**
     * 解析数据为键值对
     * @param array $column_names 列名数组
     * @param array $array 数据
     * @return mixed
     */
    private function Analysis($column_names, $array)
    {
        array_walk($array, function (&$value, $key, $data) {
            $row = array();
            $row_i = 0;
            foreach ($data as $item) {
                $data_value = $value[$row_i];
                //中文解析数据库对应ID(放到别的方法里去吧
//                if ($item == 'o_right_id') {
//                    switch ($value[$row_i]) {
//                        case '3点位':
//                            $data_value = 2;
//                            break;
//                        case '常见肿瘤':
//                            $data_value = 3;
//                            break;
//                        case '肿瘤全面':
//                            $data_value = 4;
//                            break;
//                    }
//                }
//                if ($item == 'is_used') {
//                    switch ($value[$row_i]) {
//                        case '未使用':
//                            $data_value = 0;
//                            break;
//                        case '已使用':
//                            $data_value = 1;
//                            break;
//                    }
//                }
                $row[$item] = $data_value;
                $row_i++;
            }
            $value = $row;
        }, $column_names);
        return $array;
    }


    /**
     * 保存数据
     * @param $array
     * @return int
     */
    private function saveData($array)
    {
//        $success_count = 0;//保存成功的数量
//        foreach ($array as $item) {
//            $code = new ThirdCode();//加载的数据
//            $code->load($item, '');
//            try {
//                $data_code = ThirdCode::find()->where(['code' => $code->code])->one();//数据库数据
//                if (empty($data_code)) {
//                    if ($code->is_used == 1) {
//                        $code->used_time = date('Y-m-d H:i:s');
//                    } else {
//                        $code->used_time = null;
//                    }
//                    if ($code->save()) {
//                        $success_count++;
//                    }
//                } else {
//                    $data_code->is_used = $code->is_used;
//                    if ($code->is_used == 1) {
//                        $data_code->used_time = date('Y-m-d H:i:s');
//                    } else {
//                        $data_code->used_time = null;
//                    }
//                    if ($data_code->save()) {
//                        $success_count++;
//                    }
//                }
//            } catch (Exception $e) {
//            }
//        }
//        return $success_count;
    }


}
